# -*- coding: utf-8 -*-


from indicator.template import TickIndicator


class MA(TickIndicator):
    def __init__(self, data=None, pma=5):
        super(MA, self).__init__(data, params={'pma': pma})
        self.name = 'ma' + str(pma)

    def on_ticks(self, ticks, params=None):
        lp = ticks.last_price
        ma = lp.rolling(window=self.params['pma'], min_periods=1).mean()
        ticks[self.name] = ma
        print(ma)


if __name__ == '__main__':
    from dbhelper.read_data_from_mongodb import get_ticks
    import datetime
    from indicator.template import Ticks

    ed = datetime.datetime.now() + datetime.timedelta(1)
    sd = ed - datetime.timedelta(3)

    symbol = 'rb1910'
    data = get_ticks(symbol, sd, ed)
    data.set_index('DateTime', drop=True, inplace=True)
    print(data.columns)
    dft = Ticks(data)
    print(dft.columns)
    ma = MA(dft, pma=25)
    ma.plot()
